from forest.resource import Resource, ValidationError

class Execute(object):

    def get_name(self):
        return self.value.get('name')

    def get_script(self):
        return self.value.get('script')

    def get_depends(self):
        return self.value.get('depends')


    def validate(self):
        valid_keys = ['name', 'script', 'depends']

        for k in self.value.iterkeys():
            if k not in valid_keys:
                raise ValidationError('Undefined script option {}'.format(k))

            if k == 'depends' and not isinstance(v, list):
                raise ValidationError("Depends value must be a list")

        if 'name' not in self.value:
            raise ValidationError("Script name must be defined")

class PreExecute(Resource, Execute):

    __register__ = 'pre_execute'

    def get_id(self):
        return 'pre_execute:' + str(self.get_name())

class PostExecute(Resource, Execute):

    __register__ = 'post_execute'

    def get_id(self):
        return 'post_execute:' + str(self.get_name())


